//
//  HXTactikCustomCollectionCell.m
//  GiftSay
//
//  Created by laohu on 16/1/7.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "HXTactikCustomCollectionCell.h"
#import "ProfessionTableViewCell.h"
#import "DetailContentTableViewCell.h"

@interface HXTactikCustomCollectionCell ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation HXTactikCustomCollectionCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
//        NSLog(@"%f", frame.size.height);
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(passValue:) name:@"Catagory" object:nil];
        
        [self createView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOpen" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh) name:@"NightModeOff" object:nil];
       
    }

    return self;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)refresh {
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.tacticTableView.backgroundColor = kNightModeColor;
    } else {
        self.tacticTableView.backgroundColor = [UIColor whiteColor];
    }
    [self.tacticTableView reloadData];
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    self.tacticTableView.frame = CGRectMake(0, 0, self.width, self.height);
    
}



//创建攻略tableview
- (void)createView {
//    NSLog(@"%f", self.height);
    self.tacticTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
//    self.tacticTableView.backgroundColor = [UIColor whiteColor];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        self.tacticTableView.backgroundColor = kNightModeColor;
    } else {
        self.tacticTableView.backgroundColor = [UIColor whiteColor];
    }
    self.tacticTableView.delegate = self;
    self.tacticTableView.dataSource = self;
    [self.tacticTableView registerClass:[ProfessionTableViewCell class] forCellReuseIdentifier:@"ZhuanTi"];
    [self.tacticTableView registerClass:[DetailContentTableViewCell class] forCellReuseIdentifier:@"FenLei"];
    [self addSubview:_tacticTableView];
    
}
//返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}
//返回每个区cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 1;

}
//自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
//        NSLog(@"%f", self.width);
        //scrollview
        ProfessionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZhuanTi"];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
            cell.backgroundColor = kNightModeColor;
        } else {
            cell.backgroundColor = [UIColor whiteColor];
        }
        cell.dataSource = self.tactikProfessionArray;

        
        return cell;
        
    } else {
        DetailContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FenLei"];
        if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
            cell.backgroundColor = kNightModeColor;
        } else {
            cell.backgroundColor = [UIColor whiteColor];
        }
//
        cell.model = self.tactikCatagoryArray[indexPath.section - 1];
        
        
        
        
        
        
//        NSLog(@"🐶%ld", [[self.tactikCatagoryArray[indexPath.section - 1] channel] count]);
        
        
        
        return cell;
    }
}
//section 头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 10)];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES) {
        view.backgroundColor = kNightModeColor1;
    } else {
        view.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
    }
    
    return view;
}
//section foot高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
//section头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 0.234 *self.height;
    } else {
        NSInteger k = [[self.tactikCatagoryArray[indexPath.section - 1] channel] count];
        if (k % 4 == 0) {
        return 130 * (1 + k / 4);
    } else {
        return 130 * (2 + k / 4);
        }
    }
}
//cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
